<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Model;


/**
 * @property int $id
 * @property int $user_id
 * @property int $category_id
 * @property string $title
 * @property string $cover
 * @property string $summary
 * @property string $content
 * @property string $content_md
 * @property int $read_type
 * @property int $is_stick
 * @property int $status
 * @property int $is_original
 * @property int $is_carousel
 * @property int $is_recommend
 * @property string $original_url
 * @property int $quantity
 * @property string $keywords
 * @property string $create_time
 * @property string $update_time
 */
class Article extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'article';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['user_id', 'category_id', 'title', 'cover', 'summary', 'content', 'content_md', 'read_type', 'is_stick', 'status', 'is_original', 'is_carousel', 'is_recommend', 'original_url', 'quantity', 'keywords'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'user_id' => 'integer', 'category_id' => 'integer', 'read_type' => 'integer', 'is_stick' => 'integer', 'status' => 'integer', 'is_original' => 'integer', 'is_carousel' => 'integer', 'is_recommend' => 'integer', 'quantity' => 'integer'];

    public function tags()
    {
        return $this->belongsToMany(Tag::class, 'article_tag', 'article_id', 'tag_id');
    }

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function getCoverAttribute($value)
    {
        return replaceImage($value);
    }
}
